<?php
App::uses('AppController', 'Controller');

class UsersController extends AppController {

	public $paginate = array('limit' => 25, 'conditions' => array('status' => '1'), 'order' => array('User.username' => 'asc'));

	public function beforeFilter() {
		parent::getTopic();
	}
	/**
	* admin index method
	*/
	public function index() {
		$conditions = array("User.active" => 1);
		$this->paginate = array(
                                'limit' => ITEM_PER_PAGE,
                                'order' => array('id' => 'asc'),
                             );
        $data = $this->paginate("User",$conditions);
        $this->set("data",$data);
		$this->set("strTitle","Thành viên");
	}
	/**
	* admin add method
	*/
	public function add(){
		$this->set("strTitle","Thêm Thành viên");
		if ($this -> request -> is('post')) {
			$new = $this -> data;
			$new['created'] = 	date("Y-m-d H:i:s");
			$new['modified'] = 	date("Y-m-d H:i:s");	
			$new['lastlogin'] = 	date("Y-m-d H:i:s");
			$new['creusr'] = 1;
			//$new['active'] = 1;			
			if ($this -> User -> save($new)) {
				$this -> Session -> setFlash('User "' . $new['username'] . '" add successful');
				$this -> redirect(array('action' => 'index'));
			}
			$this -> set('errors', $this -> User -> validationErrors);
		}
		$creusrs = $this->User->find('list', array(
				'fields' => array(
					'id', 'username'
				)
			));
			$this->set('creusrs', $creusrs);		
	}

	/**
	* admin edit method
	*/
	public function edit($id){
		$this->set("strTitle","Chỉnh sửa Thành viên");
		$this -> User -> id = $id;
		if (!$this -> User -> exists()) {
			$this -> Session -> setFlash('Invalid user id provided');
			 $this -> redirect(array('action' => 'index'));
		}
		
		$conditions = array("User.id" => $id);
		$data = $this->User->find('first', array('conditions' => $conditions));
		$this -> set("data", $data['User']);
		$this -> set("id",$id);
		if ($this -> request -> is('post')) {
			$update = $this -> data;
			$update['modified'] = 	date("Y-m-d H:i:s");
			$update['creusr'] = "1";
			if($this -> User -> save($update)){
				$this->Session->setFlash('User "'.$update['username'].'" update successful');
				$this->redirect(array('action' => 'index'));
			}
			$this -> set('errors', $this -> User -> validationErrors);
		}	
	    $creusrs = $this->User->find('list', array(
				'fields' => array(
					'id', 'username'
				)
			));
			$this->set('creusrs', $creusrs);	
	}
	/**
	* admin delete method
	*/
	
	public function delete($id) {
		
		$conditions = array("User.id" => $id);
		$data = $this->User->find('first', array('conditions' => $conditions));
		parent::createException($data['User']);
		$data['User']['active'] = 0;
		
		if($this -> User -> save($data['User'])){
		$this->Session->setFlash('Xóa thành công');
		}else{
			$this->Session->setFlash('Xóa không thành công');
		}
		return $this->redirect(array('controller' => 'Users', 'action' => 'index'));

	}

}
?>